package cn.itsource.basic.controller;

import cn.itsource.basic.dto.SmsCodeDto;
import cn.itsource.basic.exception.BusinessException;
import cn.itsource.basic.service.IVerifyService;
import cn.itsource.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyService verifyService;

    @GetMapping("/image/{key}")
    public AjaxResult imageVerifyCode(@PathVariable("key") String key){
        try {
            String base64Str = verifyService.imageVerifyCode(key);
            return AjaxResult.me().setResultObj(base64Str);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试!!!");
        }
    }

    @PostMapping("/smsCode")
    public AjaxResult phoneVerifyCode(@RequestBody SmsCodeDto smsCodeDto){
        try {
            verifyService.phoneVerifyCode(smsCodeDto);
            return AjaxResult.me();
        } catch (BusinessException be){
            return new AjaxResult(false,be.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试!!!");
        }
    }

    /**
     * 获取手机验证码
     * @return
     */
    @PostMapping("/binderSmsCode")
    public AjaxResult binderSmsCode(@RequestBody SmsCodeDto smsDto){
        try {
            verifyService.binderSmsCode(smsDto);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,"发送失败，" + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙!!!");
        }
    }
}
